using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;
using QFramework;
using UnityEngine;

namespace HareGame
{
    [TaskCategory("引导")]
    public class CameraFollow : Action
    {
        [Header("跟随的对象")]
        public SharedGameObject targetObj;

        [Header("跟随对象的名字(targetObj为空时生效)")]
        public string targetName;

        [Header("偏移值")]
        public Vector3 offset;

        [Header("相机广角")]
        public float size = 20;

        [Header("是否需要先移动至跟随对象位置")]
        public bool needMove = true;

        private bool _isReach = false;

        public override void OnStart()
        {
            base.OnStart();
            _isReach = false;
            if (targetObj.Value == null)
            {
                targetObj.Value = GameObject.Find(targetName);
                if (targetObj.Value == null)
                {
                    LogKit.E("找不到名为:" + targetName + "的对象");
                    return;
                }
            }

            if (needMove)
            {
                Vector3 targetPos = new Vector3(targetObj.Value.transform.position.x, Camera.main.transform.position.y, targetObj.Value.transform.position.z) + offset;
                GameEntry.Camera.Move2Pos(targetPos, size, () =>
                {
                    GameEntry.Camera.FollowObj(targetObj.Value.transform, offset, size);
                    _isReach = true;
                });
            }
            else
            {
                GameEntry.Camera.FollowObj(targetObj.Value.transform, offset, size);
                _isReach = true;
            }
        }

        public override TaskStatus OnUpdate()
        {
            return _isReach ? TaskStatus.Success : TaskStatus.Running;
        }
    }
}